<template>
  <div class="search-components">
    <div class="search-component">
      <div class="row keyword">
        <p class="label">关键词</p>
        <el-input
          v-model="searchModel.keyword"
          style="width: 220px"
          placeholder="泊位号"
        />
      </div>
      <div class="row keyword">
        <p class="label">欠费金额</p>
        <el-select
          v-model="searchModel.owedType"
          filterable
          style="width: 220px"
          placeholder="欠费金额"
        >
          <el-option
            v-for="item in owedType.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <div class="row keyword">
        <p class="label">车辆驶入时间</p>
        <el-date-picker
          style="width: 260px"
          v-model="searchModel.rangeDate"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD"
          @change="query"
        />
        <!-- <el-date-picker
          v-model="searchModel.inStartTime"
          type="date"
          placeholder="请选择开始时间"
          style="width: 160px"
          value-format="YYYY-MM-DD"
        />
        <el-date-picker
          v-model="searchModel.inEndTime"
          type="date"
          placeholder="请选择结束时间"
          style="width: 160px; margin-left: 16px"
          value-format="YYYY-MM-DD"
        /> -->
      </div>
      <div class="row">
        <p class="label">车辆驶出时间</p>
        <el-date-picker
          style="width: 260px"
          v-model="searchModel.rangeDate2"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD"
          @change="query2"
        />
        <!-- <el-date-picker
          v-model="searchModel.outStartTime"
          type="date"
          placeholder="请选择开始时间"
          style="width: 160px"
          value-format="YYYY-MM-DD"
        />
        <el-date-picker
          v-model="searchModel.outEndTime"
          type="date"
          placeholder="请选择结束时间"
          style="width: 160px; margin-left: 16px"
          value-format="YYYY-MM-DD"
        /> -->
      </div>
      <div class="search-btn">
        <el-button class="reset-btn" type="primary" @click="handleReset">
          <img class="icon-img" :src="iconReset" alt="" />
          重置</el-button
        >
        <el-button class="submit-btn" type="primary" @click="handleSearch">
          <img class="icon-img" :src="iconSearch" alt="" />
          查询</el-button
        >
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import iconSearch from "@/assets/image/btn/icon-search.png";
import iconReset from "@/assets/image/btn/icon-reset.png";
const emits = defineEmits(["search"]);

const searchModel = ref({
  keyword: "",
  owedType: "",
  rangeDate: [],
  inStartTime: "",
  inEndTime: "",
  rangeDate2: [],
  outStartTime: "",
  outEndTime: "",
});

const owedType = reactive({
  data: [
    {
      id: "",
      name: "全部",
    },
    {
      id: 1,
      name: "金额为0",
    },
    {
      id: 2,
      name: "金额不为0",
    },
  ],
});

const query = (argument) => {
  // console.log("argument ===>", argument);
  if (argument === null) {
    searchModel.value.inStartTime = "";
    searchModel.value.inEndTime = "";
    searchModel.value.rangeDate = [];
  } else {
    if (searchModel.value.rangeDate) {
      searchModel.value.inStartTime = searchModel.value.rangeDate[0];
      searchModel.value.inEndTime = searchModel.value.rangeDate[1];
      emits("search", searchModel.value);
    }
  }
};

const query2 = (argument) => {
  // console.log("argument ===>", argument);
  if (argument === null) {
    searchModel.value.outStartTime = "";
    searchModel.value.outEndTime = "";
    searchModel.value.rangeDate2 = [];
  } else {
    if (searchModel.value.rangeDate2) {
      searchModel.value.outStartTime = searchModel.value.rangeDate2[0];
      searchModel.value.outEndTime = searchModel.value.rangeDate2[1];
      emits("search", searchModel.value);
    }
  }
};

const handleReset = () => {
  searchModel.value = {};
  emits("search", searchModel.value);
};

const handleSearch = () => {
  console.log(`output-> searchModel.value`, searchModel.value);
  emits("search", searchModel.value);
};
</script>

<style lang="less" scoped>
.search-components {
  // display: flex;
  // justify-content: space-between;
  .search-component {
    display: flex;
    flex-wrap: wrap;
    .row {
      display: flex;
      align-items: center;
      margin-bottom: 16px;
    }
    .label {
      font-size: 14px;
      color: #333333;
      margin-right: 8px;
      font-weight: 400;
    }
    .keyword {
      margin-right: 32px;
    }
  }
  .search-btn {
    margin-left: 50px;
    display: flex;
    .reset-btn {
      background: #f2f3f5;
      border: 1px solid #f2f3f5;
      border-radius: 2px;
      color: #333333;
      padding: 5px 16px;
    }
    .submit-btn {
      background: #007aff;
      border: 1px solid #007aff;
      border-radius: 2px;
      padding: 5px 16px;
    }
    .icon-img {
      width: 16px;
      height: 16px;
      margin-right: 8px;
    }
  }
}
</style>